<?php

App::uses('Spot', 'Model');

/**
 * Spot Test Case
 *
 */
class SpotTestCase extends CakeTestCase {
	
	public $fixtures = array('app.spot', 'app.category', 'app.city', 'app.vote');
	
	/**
	 * setUp method
	 * @return void
	 */
	public function setUp() {
		parent::setUp();
		$this->Spot = ClassRegistry::init('Spot');
		// delete log file
		@unlink(TMP . "logs" . DS . "test.log");
		CakeLog::write('test', "SpotTestCase started.\n");
	}
	
	/**
	 * tearDown method
	 * @return void
	 */
	public function tearDown() {
		CakeLog::write('test', "SpotTestCase ended.");
		unset($this->Spot);
		parent::tearDown();
	}
	
	/**
	 * testCount
	 * verify that there are rows in the database.
	 * See SpotFixture
	 */
	public function testCount() {
		$result = $this->Spot->find('count');
		$expected = 1;	// number of rows defined in the SpotFixture
		$this->assertEqual($result, $expected);
	}
	
	public function testVirtualFields() {
		$spot = $this->Spot->findById(1);
		CakeLog::write('test', print_r($spot, true));
		$this->assertEqual($spot != null, true);
		$this->assertEqual(isset($spot['Spot']), true);
		$this->assertEqual(isset($spot['Spot']['latitude']), true);
		$this->assertEqual(isset($spot['Spot']['longitude']), true);
		$this->assertEqual(isset($spot['Spot']['category']), true);
		$this->assertEqual(isset($spot['Spot']['city']), true);
		$this->assertEqual(isset($spot['Spot']['score']), true);
	}
	
	public function testSpotScores() {
		// Spot #1 has no votes, so it should be 0.0 by default
		$spot = $this->Spot->findById(1);
		$this->assertEqual($spot['Spot']['score'], 0.0);
		// Spot #2 has 3.0 and 5.0, so it should has 4.0
		$spot = $this->Spot->findById(2);
		CakeLog::write('test', print_r($spot, true));
		$this->assertEqual($spot['Spot']['score'], 4.0);
	}
	
}